package com.hu.tool.service.importService.importServiceImpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.hu.tool.domin.TestBean;
import com.hu.tool.service.importService.ExportExcelService;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author linyh
 * @version 1.0
 * @email 1503386669@qq.com
 * @date 2021/1/27 14:57
 */
@Service
public class ExportExcelServiceImpl implements ExportExcelService {
    @Override
    public void exportExcel(HttpServletResponse response) throws IOException {
        TestBean bean1 = new TestBean();
        bean1.setName("张三");
        bean1.setAge(22);
        bean1.setPass(true);
        bean1.setScore(66.30);
        bean1.setExamDate(DateUtil.date());

        TestBean bean2 = new TestBean();
        bean2.setName("李四");
        bean2.setAge(28);
        bean2.setPass(false);
        bean2.setScore(38.50);
        bean2.setExamDate(DateUtil.date());

        List<TestBean> rows = CollUtil.newArrayList(bean1, bean2);

        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(true);

        //自定义标题别名
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("age", "年龄");
        writer.addHeaderAlias("score", "分数");
        writer.addHeaderAlias("isPass", "是否通过");
        writer.addHeaderAlias("examDate", "考试时间");

        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(4, "一班成绩单");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);

        // 设置背景颜色
        StyleSet style = writer.getStyleSet();
        style.setBackgroundColor(IndexedColors.RED, false);
        // 头部样式
        CellStyle headCellStyle = style.getHeadCellStyle();
        headCellStyle.setBorderBottom(BorderStyle.DOUBLE);
        // 日期单元格样式
        CellStyle cellStyleForDate = style.getCellStyleForDate();
        cellStyleForDate.setBorderBottom(BorderStyle.DASH_DOT_DOT);
        // 数值单元格样式
        CellStyle cellStyleForNumber = style.getCellStyleForNumber();
        cellStyleForNumber.setBorderBottom(BorderStyle.MEDIUM_DASHED);
        // 普通单元格样式
        CellStyle cellStyle = style.getCellStyle();
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);
        // 关闭writer，释放内存
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        response.setHeader("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode("成绩单.xlsx", "UTF-8"));
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }
}
